package com.raising.framework.filter;

import com.raising.utils.EncodesUtils;
import com.raising.utils.JStringUtils;
import org.apache.shiro.web.filter.authc.UserFilter;
import org.apache.shiro.web.util.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class MyUserFilter extends UserFilter {

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest re = (HttpServletRequest)request;
        String query = re.getQueryString();
        StringBuffer oldUrl = re.getRequestURL();
        if(JStringUtils.isNotBlank(query)){
            oldUrl = oldUrl.append("?").append(query);
        }
        String jumpUrl = EncodesUtils.urlEncode(oldUrl.toString());

        saveRequest(request);
        String loginUrl = getLoginUrl();
        if(JStringUtils.isNotBlank(jumpUrl)){
            loginUrl = loginUrl + (loginUrl.contains("?") ? "&" : "?") + "jumpUrl="+jumpUrl;
        }
        WebUtils.issueRedirect(request, response, loginUrl);

        return false;
    }
}
